//
//  YFSystemTemplateCell.m
//  SquashSpark
//
//  Created by Coollang on 2017/11/29.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFSystemTemplateCell.h"
#import "YFThreeDTemplate.h"
#import "YFSystemTemplateSubView.h"

NSString * SystemCollectionCellID = @"YFSystemTemplateSubView";
#define ItemSizeWidth kAutoWid(311)
#define KCellSpace    40 //cell之间的间距

@interface YFSystemTemplateCell()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
/** 当前滚动到的页数 */
@property (nonatomic, assign) NSInteger cureentPage;

/** 系统模板 */
@property (nonatomic, strong) NSArray *systemTemplateArr;
@end

@implementation YFSystemTemplateCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [self configuerCollection];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if ([User getLastUser].currentSelectTemplate.mainType == TemplateMainTypeSystem) {
            NSInteger index = [self.systemTemplateArr indexOfObject:[User getLastUser].currentSelectTemplate];
            if (index < self.systemTemplateArr.count) {
                [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] atScrollPosition:(index == self.systemTemplateArr.count - 1) ? UICollectionViewScrollPositionRight:UICollectionViewScrollPositionLeft animated:YES];
            }
        }
    });
    
}

- (void)configuerCollection {
    [self.collectionView registerNib:[UINib nibWithNibName:SystemCollectionCellID bundle:nil] forCellWithReuseIdentifier:SystemCollectionCellID];
    self.collectionView.backgroundColor = kSeparationLineColor;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.flowLayout.itemSize = CGSizeMake(ItemSizeWidth, kAutoHei(209));
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.flowLayout.minimumLineSpacing = 7;
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.sectionInset = UIEdgeInsetsZero;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.systemTemplateArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
     YFSystemTemplateSubView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:SystemCollectionCellID forIndexPath:indexPath];
    YFThreeDTemplate *templateModel = self.systemTemplateArr[indexPath.item];
        UIImage *image = [UIImage imageNamed:templateModel.templateImage];
    NSString *typeStr = [YFThreeDTemplate templateWithHandType:templateModel.handType selectActionType:templateModel.selectActionType];
    [cell configuerViewWithTitle:templateModel.name subTitle:typeStr bgImage:image];
//    [cell configuerViewWithTitle:templateModel.name subTitle:templateModel.threeDItem.typeString bgImage:image];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.didSelectSystemTemplateBlock) {
        YFThreeDTemplate *model = [self.systemTemplateArr objectAtIndex:indexPath.item];
        self.didSelectSystemTemplateBlock(indexPath.item, model);
    }
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

- (NSArray *)systemTemplateArr {
    if (_systemTemplateArr == nil) {
        _systemTemplateArr = (NSArray *)[YFThreeDTemplate getCurrentUserAllSystemTemplateModels];
    }
    return _systemTemplateArr;
}

@end


